﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.Account;
using System.IO;
using FisheryPlatform.Bussiness.LoginCookie;
using FisheryPlatform.Common.StaticCache;
using FisheryPlatform.Bussiness.Cache;
using FisheryPlatform.Bussiness.FileStore;

namespace FisheryPlatform.Web.Admin.Account
{
    public partial class Edit : System.Web.UI.Page
    {
        protected UsersEntity UserInfo = null;
        protected int flag = 0;
        protected bool UpdateSelf = false;
        protected LoginUserInfo CurrentUserInfo = new LoginUserInfo();
        protected bool isNewAdd = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            if (string.Equals(Path.GetFileNameWithoutExtension(Request.Url.LocalPath), "Profile", StringComparison.OrdinalIgnoreCase))
            {
                UpdateSelf = true;
            }

            if (string.IsNullOrEmpty(Request.QueryString["userId"]))
            {
                isNewAdd = true;
            }

            if (IsPostBack)
            {
                Save();
            }
            #endregion
        }

        private void Save()
        {
            #region
            UserInfo = new UsersEntity();
            if (UpdateSelf)
            {
                UserInfo.UserId = CurrentUserInfo.UserID;
            }
            else
            {
                if (isNewAdd == false)
                {
                    UserInfo.UserId = long.Parse(Request.QueryString["userId"]);
                }
            }
            UserInfo.LoginName = txt_LoginName.Value.Trim();
            UserInfo.RealName = txt_Name.Value.Trim();
            UserInfo.EMail = txt_EMail.Value.Trim();
            UserInfo.Mobile = txt_Mobile.Value.Trim();
            UserInfo.Phone = txt_Phone.Value.Trim();
            UserInfo.Password = txt_Password.Value.Trim();
            if (isNewAdd)
            {
                UserInfo.Status = 1;
            }

            if (string.IsNullOrEmpty(txt_LoginName.Value.Trim()) == false
                && string.IsNullOrEmpty(txt_Name.Value) == false
                && string.IsNullOrEmpty(txt_Mobile.Value) == false
                && txt_Password.Value.Trim() == txt_ComfirmPassword.Value.Trim()
                && UserManager.Save(UserInfo))
            {
                if (isNewAdd == false && UserInfo.Password.Length >= 6)
                {
                    UserManager.UpdatePassword(UserInfo.UserId, UserInfo.Password);
                }
                if (UserInfo.UserId == CurrentUserInfo.AdminUserInfo.UserId)
                {
                    CacheDataUtility.Intance.Delete(CacheKeyConstants.AdminUserInfoConstant);
                    ProductStaticHtmlUploadProvider.DeleteAllFile();
                }
                flag = 1;
            }
            else
            {
                flag = 2;
            }
            #endregion
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            #region
            if (IsPostBack == false)
            {
                long userId = 0;
                if (long.TryParse(Request.QueryString["userId"], out userId) || UpdateSelf)
                {
                    UserInfo = UserManager.GetUserInfo(UpdateSelf ? CurrentUserInfo.UserID : userId);
                    txt_LoginName.Value = UserInfo.LoginName;
                    txt_LoginName.Disabled = true;
                    txt_Name.Value = UserInfo.RealName;
                    txt_EMail.Value = UserInfo.EMail;
                    txt_Mobile.Value = UserInfo.Mobile;
                    txt_Phone.Value = UserInfo.Phone;
                }
                else
                {
                    UserInfo = new UsersEntity();
                }
            }
            #endregion
        }
    }
}